package com.ponhu.ea2.goodsCenter.provider.schedule;

import com.google.common.collect.Lists;
import com.ponhu.ea2.common.basic.model.ResponseBean;
import com.ponhu.ea2.goodsCenter.provider.mapper.GoodsCreateOrUpdateForWMSMapper;
import com.ponhu.ea2.wmscenter.facade.bms.PlatformFacade;
import com.ponhu.ea2.wmscenter.model.bms.dto.GoodsCreateOrUpdateDto;
import com.ponhu.ea2.wmscenter.model.bms.dto.inner.GoodsCreateOrUpdateItemDto;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Spliterator;

/**
 * @author luyu
 * @create 2021-08-30 15:38
 */
@Slf4j
@Component
public class GoodsCreateOrUpdateForWMSTask {
    @Resource
    GoodsCreateOrUpdateForWMSMapper goodsCreateOrUpdateForWMSMapper;

    @DubboReference(version = "1.0.0", check = false)
    PlatformFacade platformFacade;

    /**
     * List分割条数
     */
    private final Integer LIST_LIMIT = 2000;

    @XxlJob("goodscreateOrUpdateTask")
    public ResponseBean goodscreateOrUpdateTask(String param) {
        ResponseBean responseBean = null;
        try {
            // 查询所有需要推送的数据
            List<GoodsCreateOrUpdateItemDto> list  = goodsCreateOrUpdateForWMSMapper.queryGoodsList();
            List<List<GoodsCreateOrUpdateItemDto>> partition = Lists.partition(list, LIST_LIMIT);
            for (List<GoodsCreateOrUpdateItemDto> goodsCreateOrUpdateItemDtos : partition) {
                GoodsCreateOrUpdateDto goodsCreateOrUpdateDto = new GoodsCreateOrUpdateDto();
                goodsCreateOrUpdateDto.setItems(goodsCreateOrUpdateItemDtos);
                responseBean = platformFacade.subGoodsCreateOrUpdate(goodsCreateOrUpdateDto);
                log.debug("推送wms商品信息结果:{}",responseBean.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseBean;
    }
}
